package com.example.init.controller.HTTPController;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@RestController
@RequestMapping("/cookie")
public class CookieController {

    @RequestMapping("/setCookie")
    public String  setCookie(HttpServletRequest request,HttpServletResponse response){
      Cookie cookie = new Cookie("echo","111");
       response.addCookie(cookie);
       return "set success";
    }

    @RequestMapping("/getCookie")
    public String  getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        //使用 Lambda 表达式方式遍历
        if(cookies != null){
            Arrays.stream(cookies).forEach(cookie -> {
                System.out.println(cookie.getName() + ":" + cookie.getValue());
            });
        }
        return "获取 Cookie 成功";
    }

    /**
     * String类型的参数名需要和 客户端存储的 cookie 的值名字相同
     * @param wen
     * @param liu
     * @return
     */
    @RequestMapping("/cookie2")
    public String getCookie2(@CookieValue String wen,@CookieValue String liu){
        return "cookie 存取的值：" + wen + "  " + liu;
    }

}
